package com.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution04 {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> tmp = new LinkedList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum3(int k, int n) {
        backTracking(k,n,1);
        return res;
    }

    public void backTracking(int k,int n, int start) {
        if (sum > n) return;
        if (tmp.size() == k) {
            if (sum == n)
                res.add(new ArrayList<>(tmp));
            return;
        }

        for (int i = start; i <= 9 - (k-tmp.size())+1 ; i++) {
            tmp.add(i);
            sum += i;
            backTracking(k,n,i+1);
            tmp.removeLast();
            sum -= i;
        }
    }
}
